#ifndef __LOCK_H__
#define __LOCK_H__

#include "Nocopy.h"

#if defined(_DEBUG)
#define _DEBUG_LOCKOBJECT
#endif

class LockObject
{
protected:
	class Impl;
	Impl * impl;

	friend class Lock;

#if defined(_DEBUG_LOCKOBJECT)
	void lock_object_dbg(char * szFile, int nLine) const;
#else
	void lock_object() const;
#endif

	void unlock_object() const;

public:
	LockObject();
	LockObject(const LockObject & obj);
	const LockObject & operator=(const LockObject & obj);
	~LockObject();
};

class Lock : private Nocopy
{
	const LockObject * m_pLockObject;
public:
#if defined(_DEBUG_LOCKOBJECT)
	Lock(const LockObject * pLockObject, char *szFile = "<unknown>", int nLine = -1): 
		m_pLockObject(pLockObject)
	{
		m_pLockObject->lock_object_dbg(szFile, nLine);
	}
#else
	Lock(const LockObject * pLockObject): 
		m_pLockObject(pLockObject)
	{
		m_pLockObject->lock_object();
	}
#endif

	~Lock()
	{
		m_pLockObject->unlock_object();
	}
};

#if defined(_DEBUG_LOCKOBJECT)
#	define  lock_object()	lock_object_dbg(__FILE__, __LINE__)
#endif

#if	defined(_DEBUG_LOCKOBJECT)
//#	define LOCK(p) Lock __lock__((p), __FILE__, __LINE__)
#	define LOCKARGS(p)	(p),__FILE__,__LINE__
#else
//#	define LOCK(p) Lock __lock__((p))
#	define LOCKARGS(p)	(p)
#endif
#endif //__LOCK_H__